package leaveSystem.bizlogic;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import leaveSystem.dao.DAOException;
import leaveSystem.dao.DAOFactory;
import leaveSystem.dao.EmployeeDAO;
import leaveSystem.dao.EmployeeWorkscheduleDAO;
import leaveSystem.dao.LeaveDAO;
import leaveSystem.dao.LeaveTypeDAO;
import leaveSystem.dao.PublicHolidaysDAO;
import leaveSystem.dao.RoleDAO;
import leaveSystem.model.Employee;
import leaveSystem.model.Leave;
import leaveSystem.model.LeaveType;
import leaveSystem.model.PublicHolidays;

public class LeaveManager {
	private EmployeeDAO EmployeeDAO;
	private EmployeeWorkscheduleDAO EmployeeWorkscheduleDAO;
	private LeaveDAO LeaveDAO;
	private LeaveTypeDAO LeaveTypeDAO;
	private PublicHolidaysDAO PublicHolidaysDAO;
	private RoleDAO RoleDAO;
	//for database connection
	private static Connection conn;
	
	public LeaveManager(){
		EmployeeDAO = DAOFactory.loadInstance().getEmployeeDAO();
		EmployeeWorkscheduleDAO = DAOFactory.loadInstance().getEmployeeWorkscheduleDAO();
		LeaveDAO = DAOFactory.loadInstance().getLeaveDAO();
		LeaveTypeDAO = DAOFactory.loadInstance().getLeaveTypeDAO();
		PublicHolidaysDAO = DAOFactory.loadInstance().getPublicHolidaysDAO();
		RoleDAO = DAOFactory.loadInstance().getRoleDAO();
		//create connection
		conn = DAOFactory.getDatabaseConnection();
	}
	//Login Employee
	public List<Employee> checkEmployeeLogin(Employee user){
		List<Employee> currentEmployee=null;
		try {
			System.out.println("details are "+user.getUsername()+" and passwword is "+user.getPassword());
			currentEmployee = EmployeeDAO.findEmployee(conn, user);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		return currentEmployee;
	}
	
	public void createLeave(Leave leave){
System.out.println("leavemanager createLeave");
		 try {
			LeaveDAO.insertLeave(conn, leave);
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }
	
	public void updateLeave(Leave leave){
		 try {
			LeaveDAO.updateLeave(conn, leave);
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }	

	public List<Leave> getStaffsLeave(Leave leave){
		List<Leave> leaveRecord=null; 
		try {
			leaveRecord = LeaveDAO.findLeave(conn, leave);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return leaveRecord;
	 }

// Employee DAO 
//	public void createEmployee(Employee emp){
//		 try {
//		 EmployeeDAO.insertEmployee(conn, emp);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	 }	

	public String updateEmployee(Employee emp){
		String isUpdated="true";
		 try {
		    EmployeeDAO.updateEmployee(conn, emp);
		   } catch (Exception e) {
			e.printStackTrace();
			isUpdated="false";
		}
		return isUpdated;
	 }	
	public List<Employee> getStaff(Employee emp){
		List<Employee> empRecord=null; 
		try {
			empRecord = EmployeeDAO.findEmployee(conn, emp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return empRecord;
	 }

//LeaveTypeDAO
	public void createLeaveType(LeaveType leaveType){
		 try {
			 LeaveTypeDAO.insertLeaveType(conn, leaveType);
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }	
	public void updateLeaveType(LeaveType leaveType){
		 try {
		    LeaveTypeDAO.updateLeaveType(conn, leaveType);
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }	


	public List<LeaveType> getLeaveType(LeaveType leaveType){
		List<LeaveType> leaveTypeRecord=null; 
		try {
			leaveTypeRecord = LeaveTypeDAO.findLeaveType(conn, leaveType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return leaveTypeRecord;
	 }

//PublicHolidaysDAO

	public void createPublicHolidays(PublicHolidays phdays){
		 try {
			PublicHolidaysDAO.insertPublicHolidays(conn, phdays);
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }
	
	public void updatePublicHolidays(PublicHolidays phdays){
		 try {
			 PublicHolidaysDAO.updatePublicHolidays(conn, phdays);
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }	


	public List<PublicHolidays> getPublicHolidays(PublicHolidays phdays){
		List<PublicHolidays> phdaysRecord=null; 
		try {
			phdaysRecord = PublicHolidaysDAO.findPublicHolidays(conn, phdays);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return phdaysRecord;
	 }
		
	public int countNoOfLeave(Leave leave){
		int count;
		 try {
				count = LeaveDAO.countDaysByLeaveType(conn, leave);
				System.out.println(count);
				return count;
			} catch (Exception e) {
				e.printStackTrace();
			}
		return 0;
	}
	public String createEmployee(Employee emp){
		String result = "Saving Successful.";
		 try {
		 EmployeeDAO.insertEmployee(conn, emp);
		}catch (com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException sqle){
			if(	sqle.getMessage().contains("Duplicate")){
				result = "Employee ID already exist.";
			} else {
				sqle.printStackTrace();
				result = "Unknow error occured. Please contact system admin.";
			}
		}catch (Exception e) {			
			e.printStackTrace();
			result = "Unknow error occured. Please contact system admin.";
		} 		
		return result;
	 }
	@SuppressWarnings("finally")
	public ArrayList<Employee> findAllEmployee() {
    	ArrayList<Employee> currentList = new ArrayList<Employee>();
		try {
			currentList = EmployeeDAO.findAllEmployee(conn);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			return currentList;
		}
    }  
	
	
	@SuppressWarnings("finally")
	public Employee findEmployeeById(Integer employeeID){
		Employee currentEmployee = null;
		try {
			currentEmployee = EmployeeDAO.findEmployeeById(conn,employeeID);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			return currentEmployee;
		}
		
	}

	public void deleteEmployee(Employee employee) {
    	try {
    		try {
				EmployeeDAO.deleteEmployee(conn,employee);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	public List<PublicHolidays> getAllPublicHolidays(){
		List<PublicHolidays> phdaysRecord=null; 
		try {
			phdaysRecord = PublicHolidaysDAO.loadAll(conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return phdaysRecord;
	 }
	public List<PublicHolidays> getHolidaysByDates(String start,String end){
		List<PublicHolidays> phdaysRecord=null; 
		try {
			phdaysRecord = PublicHolidaysDAO.findPublicHolidaysByDates(conn,start,end);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return phdaysRecord;
	}
	
	public List<Leave> getLeaveByEmpId(int empid){
		List<Leave> records = null;
		try {
			records = LeaveDAO.findLeaveByEmpId(conn, empid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return records;
	}
}
